package com.jaya.budan.utils;

import com.jaya.budan.bean.COS_GetToken;
import com.tencent.qcloud.core.auth.BasicLifecycleCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudLifecycleCredentials;
import com.tencent.qcloud.core.auth.SessionQCloudCredentials;
import com.tencent.qcloud.core.common.QCloudClientException;

public class MyCredentialProvider extends BasicLifecycleCredentialProvider {

    private COS_GetToken mgetToken;
    public MyCredentialProvider(COS_GetToken getToken){
        mgetToken = getToken;
    }

    @Override
    protected QCloudLifecycleCredentials fetchNewCredentials() throws QCloudClientException {

        // 首先从您的临时密钥服务器获取包含了签名信息的响应

        // 然后解析响应，获取密钥信息
        String tmpSecretId = mgetToken.getCredentials().getTmpSecretId(); //临时密钥 secretId
        String tmpSecretKey = mgetToken.getCredentials().getTmpSecretKey(); //临时密钥 secretKey
        String sessionToken = mgetToken.getCredentials().getSessionToken(); //临时密钥 Token
        long expiredTime = mgetToken.getExpiredTime();//临时密钥有效截止时间戳，单位是秒

        /*强烈建议返回服务器时间作为签名的开始时间，用来避免由于用户手机本地时间偏差过大导致的签名不正确 */
        // 返回服务器时间作为签名的起始时间
        long startTime = mgetToken.getStartTime(); //临时密钥有效起始时间，单位是秒

        // todo something you want

        // 最后返回临时密钥信息对象
        return new SessionQCloudCredentials(tmpSecretId, tmpSecretKey, sessionToken, startTime, expiredTime);
    }
}
